# coding=utf-8
from tkinter import *
import tkinter.filedialog  # 文件
import tkinter.messagebox  # 弹窗
import os
import platform
from WordzzzInter import *
from watchs import StopWatch

varA = None
varW = None
lines = None
wei = None
wDic = {"-f": "", "-t": "c2e", "-n": 0, "-l": "", "-o": "", "-w": "", "-a": "", "-r": ""}
# https://www.cnblogs.com/wangdianchao/p/11521825.html

# 画布创建
hb = Tk()  # 创建画布
hb.title('Wordzzz')  # 设置画布标题
# 设置画布大小 与 屏幕居中
sysStr = platform.system()  # 获得操作系统类型
if sysStr == "Windows":
    fontSize = 3
    width = 272
    heigh = 272
else:
    width = 272
    heigh = 260
    fontSize = 0
screenwidth = hb.winfo_screenwidth()
screenheight = hb.winfo_screenheight()
hb.geometry('%dx%d+%d+%d' % (width, heigh, (screenwidth - width) / 2, (screenheight - heigh) / 2))
hb.resizable(width=False, height=False)  # 高宽不可变
# hb['bg'] = '#0099ff'
hb.attributes('-topmost', -1)  # 置顶窗口


def watch():
    lfTime = LabelFrame(hb, text='计时器')
    lfTime.grid()
    sw = StopWatch(lfTime)
    sw.stopwatch()
    lfTime.place(x=212, y=44)
    # lfTime.grid(row=1, column=2)


watch()


def runWordzzz(wDic):
    run(wDic)
    # cmd = "python Wordzzz.py"
    # for key in wDic:
    #     if wDic[key] != "":
    #         # print(key,wDic[key])
    #         cmd += " "+key+" "+wDic[key]
    # print(cmd)
    # os.system(cmd)
    # tkinter.messagebox.showinfo('通知','本次默写完成！')
    print("---------------------------------------")
    print("请继续选择功能:")
    print("---------------------------------------\n")


# 最终选择
def go():
    if wDic["-f"] == "":
        tkinter.messagebox.showwarning('警告', '必须选择文件！')
        return
    if varA is not None:
        if varA.get():
            wDic["-a"] = "T"
            varA.set(False)
    if varW is not None:
        if varW.get():
            wDic["-w"] = "T"
            varW.set(False)
    if lines is not None:
        if lines.get() != "":
            wDic["-l"] = lines.get()
            lines.set("")
    if wei is not None:
        if wei.get() != 0:
            wDic["-o"] = wei.get()
            wei.set(0)
            # wei.delete(0, END)
    wDic["-t"] = var1.get()
    if number.get() is not None and number.get() != 0:
        wDic["-n"] = number.get()
        number.set(0)
    else:
        wDic["-n"] = "0"
    # print(wDic)
    runWordzzz(wDic)
    wDic["-l"] = ""
    wDic["-w"] = ""
    wDic["-a"] = ""
    wDic["-o"] = ""


# 重置
def reset():
    # reDic = {"-f":"", "-t":"", "-n":0, "-l":"", "-o":"", "-w":"", "-a":"", "-r":""}
    if wDic["-f"] == "":
        tkinter.messagebox.showwarning('警告', '必须选择文件！')
        return
    # reDic['-f'] = wDic['-f']
    if tkinter.messagebox.askyesno(title='注意', message='是否重置权重?'):
        wDic["-r"] = "T"
        run(wDic)
        print("---------------------------------------")
        print("请继续选择功能:")
        print("---------------------------------------\n")
    else:
        wDic["-r"] = False
    # tkinter.messagebox.showinfo('通知','重置完成！')
    wDic["-r"] = ""


# 文件选择
def getFile():  # 获取Excel
    fileDir = tkinter.filedialog.askopenfilename()  # Excel路径
    fileName = os.path.basename(fileDir)
    fileVar.set("当前文件: " + fileName)  # 添加文件名
    wDic['-f'] = fileDir


choseExcel = Button(hb, text='选择Excel文件', bg="green", relief=tkinter.FLAT, font=('黑体', 10 - fontSize), width=12,
                    height=2,
                    command=getFile)
choseExcel.place(x=8, y=8)
fileVar = StringVar()
fileVar.set("当前文件: 未选择")
labFile = Label(hb, textvariable=fileVar, font=('黑体', 11 - fontSize))  # 显示文件名

# 默写类型选择框架
var1 = StringVar()
var1.set("c2e")  # 设置默认
lf1 = LabelFrame(hb, text='1.选择默写类型')  # 定义框架
c2e = Radiobutton(lf1, text="汉译英", value="c2e", variable=var1).grid()
e2c = Radiobutton(lf1, text="英译汉", value="e2c", variable=var1).grid()
lf1.place(x=8, y=42)


def getVar2():  # 根据默写范围调整框架3
    for widget in lf3.winfo_children():  # 清空框架
        widget.destroy()
    # btn2.grid_forget()
    if var2.get() == "default":
        # -w -a -n
        global varW
        global varA
        varW = BooleanVar()  # 多选框选中状态，可以是BooleanVar()或者IntVar()
        varA = BooleanVar()
        Checkbutton(lf3, text="按权重默", variable=varW).grid()
        Checkbutton(lf3, text="修改权限", variable=varA).grid()
    if var2.get() == "lines":
        global lines
        Label(lf3, text="行数范围:", font=('黑体', 10)).pack(anchor=W)
        lines = StringVar()
        Entry(lf3, width=8, textvariable=lines).pack(anchor=S)  # 单行文本框 获取行数
    if var2.get() == "onlyWeight":
        global wei
        Label(lf3, text="权重值:", font=('黑体', 10)).pack(anchor=W)
        wei = IntVar()
        Entry(lf3, width=8, textvariable=wei).pack(anchor=S)  # 单行文本框 获取行数


# 默写类型选择框架2
var2 = StringVar()
var2.set("default")  # 设置默认
lf2 = LabelFrame(hb, text='2.选择默写范围')  # 定义框架
radio1 = Radiobutton(lf2, text="普通默写", value="default", variable=var2, command=getVar2).grid()
radio2 = Radiobutton(lf2, text="行数默写", value="lines", variable=var2, command=getVar2).grid()
radio3 = Radiobutton(lf2, text="权重默写", value="onlyWeight", variable=var2, command=getVar2).grid()
# btn2 = Button(lf2, text='确认选择', command=getVar2)
# btn2.grid()

lf3 = LabelFrame(hb, text='3.具体参数')  # 定义框架
varW = BooleanVar()  # 多选框选中状态，可以是BooleanVar()或者IntVar()
varA = BooleanVar()
Checkbutton(lf3, text="按权重默", variable=varW).grid()
Checkbutton(lf3, text="修改权限", variable=varA).grid()


# 退出按钮
def exit():
    if tkinter.messagebox.askyesno(title='注意', message='是否退出?'):
        hb.quit()


exitBtn = tkinter.PhotoImage(file='ico/exit.png')
extBtn = Button(image=exitBtn, command=exit)

# https://blog.csdn.net/qq_41556318/article/details/85112829
lf4 = LabelFrame(hb, text='4.默写单词数量')
# Label(lf4,text='默写词数:').grid()
number = IntVar()
Entry(lf4, width=10, textvariable=number).grid()  # 默写单词数量


if sysStr == "Windows":
    startW = 10
    startH = 3
    resetW = 10
    resetH = 2
else:
    startW = 9
    startH = 4
    resetW = 9
    resetH = 3
# 开始重置按钮
startBtn = Button(hb, text="开始默写", bg="red", command=go, width=startW, height=startH)
resetBtn = Button(hb, text="重置权重", command=reset, width=resetW, height=resetH)

# 放置控件
if sysStr == "Windows":
    labFile.place(x=82, y=15)   # 文件名
    lf2.place(x=8, y=120)  # 框架2
    lf3.place(x=120, y=124)  # 框架3
    lf4.place(x=8, y=225)  # 框架4
    extBtn.place(x=212, y=214)  # 退出按钮
    startBtn.place(x=120, y=48)  # 开始按钮
    resetBtn.place(x=120, y=218)  # 重置按钮
else:
    labFile.place(x=98, y=13)   # 文件名
    lf2.place(x=8, y=110)  # 框架2
    lf3.place(x=120, y=120)  # 框架3
    lf4.place(x=8, y=200)  # 框架4
    extBtn.place(x=212, y=193)  # 退出按钮
    startBtn.place(x=120, y=46)  # 开始按钮
    resetBtn.place(x=120, y=195)  # 重置按钮

hb.mainloop()  # 放置画布
